package de.blitzcoder.cddatabase.data;

import java.io.Serializable;

/**
 *
 * @author blitzcoder
 */
public class Track implements Serializable {

    public static final String  NO_ARTIST = new String("");

    private String name;
    private String artist;
    private int duration; // Seconds

    public Track(String name, String artist, int duration) {
        this.name = name;
        this.duration = duration;
        setArtist(artist);
    }

    public int getDuration() {
        return this.duration;
    }
    
    public void setDuration(int duration) {
        this.duration = duration;
    }

    public void setDuration(String duration) {
        String[] numbers = duration.split(":");
        if (numbers.length == 0 || numbers.length>3)
            throw new NumberFormatException();
        if (numbers.length == 1) {
            this.duration = Integer.parseInt(numbers[0]);
        }
        if (numbers.length == 2) {
            int min = Integer.parseInt(numbers[0]);
            int sec = Integer.parseInt(numbers[1]);
            this.duration = (min*60)+sec;
        }
        if (numbers.length == 3) {
            int hour = Integer.parseInt(numbers[0]);
            int min = Integer.parseInt(numbers[1]);
            int sec = Integer.parseInt(numbers[2]);
            this.duration = (hour*3600)+(min*60)+sec;
        }
    }

    public static String convertSecondsToString(int sec) {
        int hours = sec / 3600;
        sec = sec % 3600;
        int min = sec / 60;
        sec = sec % 60;
        String str = "";
        if (hours != 0)
            str+=hours+":";
        str+=addZero(min, 2)+":";
        str+=addZero(sec, 2);
        return str;
    }
    
    private static String addZero(int num, int chars) {
        String str = ""+num;
        for (int i=0;i<chars-str.length();i++) {
            str="0"+str;
        }
        return str;
    }

    public void setArtist(String artist) {
        if (artist == null || artist.length() == 0)
            this.artist = NO_ARTIST;
        else
            this.artist = artist;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public String getArtist() {
        return this.artist;
    }

    public Track(String name) {
        this(name, null, 0);
    }

}